1"use strict";(globalThis.webpackChunkdocs=globalThis.webpackChunkdocs||[]).push([[2116],{56393(e,r,t){t.r(r),t.d(r,{assets:()=>p,contentTitle:()=>c,default:()=>u,frontMatter:()=>o,metadata:()=>i,toc:()=>d});const i=JSON.parse('{"id":"libraries/standard_library/cryptographic_primitives/ciphers","title":"Ciphers","description":"Learn about the implemented ciphers ready to use for any Noir project","source":"@site/versioned_docs/version-v1.0.0-rc.1/libraries/standard_library/cryptographic_primitives/ciphers.mdx","sourceDirName":"libraries/standard_library/cryptographic_primitives","slug":"/libraries/standard_library/cryptographic_primitives/ciphers","permalink":"/docs/v1.0.0-rc.1/libraries/standard_library/cryptographic_primitives/ciphers","draft":false,"unlisted":false,"editUrl":"https://github.com/noir-lang/noir/edit/master/docs/versioned_docs/version-v1.0.0-rc.1/libraries/standard_library/cryptographic_primitives/ciphers.mdx","tags":[],"version":"v1.0.0-rc.1","frontMatter":{"title":"Ciphers","description":"Learn about the implemented ciphers ready to use for any Noir project","keywords":["ciphers","Noir project","aes128","encrypt"]},"sidebar":"sidebar","previous":{"title":"Cryptographic Primitives","permalink":"/docs/v1.0.0-rc.1/libraries/standard_library/cryptographic_primitives/"},"next":{"title":"Scalar multiplication","permalink":"/docs/v1.0.0-rc.1/libraries/standard_library/cryptographic_primitives/embedded_curve_ops"}}');var n=t(74848),s=t(28453),a=t(8037);const o={title:"Ciphers",description:"Learn about the implemented ciphers ready to use for any Noir project",keywords:["ciphers","Noir project","aes128","encrypt"]},c=void 0,p={},d=[{value:"aes128",id:"aes128",level:2}];function l(e){const r={blockquote:"blockquote",code:"code",h2:"h2",p:"p",pre:"pre",...(0,s.R)(),...e.components};return(0,n.jsxs)(n.Fragment,{children:[(0,n.jsx)(r.h2,{id:"aes128",children:"aes128"}),"\n",(0,n.jsxs)(r.p,{children:["Given a plaintext as an array of bytes, returns the corresponding aes128 ciphertext (CBC mode). Input padding is automatically performed using PKCS#7, so that the output length is ",(0,n.jsx)(r.code,{children:"input.len() + (16 - input.len() % 16)"}),"."]}),"\n",(0,n.jsx)(r.pre,{children:(0,n.jsx)(r.code,{className:"language-rust",metastring:'title="aes128" showLineNumbers ',children:"/// Given a plaintext as an array of bytes, returns the corresponding aes128 ciphertext (CBC mode). Input padding is performed using PKCS#7, so that the output length is `input.len() + (16 - input.len() % 16)`.\npub fn aes128_encrypt<let N: u32>(\n input: [u8; N],\n iv: [u8; 16],\n key: [u8; 16],\n) -> [u8; N + 16 - N % 16] {\n let padding_length = (16 - N % 16) as u8;\n let mut padded_input: [u8; N + 16 - N % 16] = [0; N + 16 - N % 16];\n for i in 0..N {\n padded_input[i] = input[i];\n }\n for i in N..N + 16 - N % 16 {\n padded_input[i] = padding_length;\n }\n let output = aes128_encrypt_padded_input(padded_input, iv, key);\n output\n}\n\n#[foreign(aes128_encrypt)]\nfn aes128_encrypt_padded_input<let N: u32>(input: [u8; N], iv: [u8; 16], key: [u8; 16]) -> [u8; N] {}\n"})}),"\n",(0,n.jsxs)(r.blockquote,{children:["\n",(0,n.jsx)("sup",{children:(0,n.jsx)("sub",{children:(0,n.jsx)("a",{href:"https://github.com/noir-lang/noir/blob/master/noir_stdlib/src/aes128.nr#L1-L23",target:"_blank",rel:"noopener noreferrer",children:"Source code: noir_stdlib/src/aes128.nr#L1-L23"})})}),"\n"]}),"\n",(0,n.jsx)(r.pre,{children:(0,n.jsx)(r.code,{className:"language-rust",children:"fn main() {\n let input: [u8; 4] = [0, 12, 3, 15]; // Random bytes, will be padded to 16 bytes.\n let iv: [u8; 16] = [0; 16]; // Initialization vector\n let key: [u8; 16] = [0; 16]; // AES key\n let ciphertext = std::aes128::aes128_encrypt(input, iv, key); // In this case, the output length will be 16 bytes.\n}\n"})}),"\n",(0,n.jsx)(a.A,{to:"../black_box_fns"})]})}function u(e={}){const{wrapper:r}={...(0,s.R)(),...e.components};return r?(0,n.jsx)(r,{...e,children:(0,n.jsx)(l,{...e})}):l(e)}},8037(e,r,t){t.d(r,{A:()=>s});var i=t(28774),n=t(74848);function s({to:e}){return(0,n.jsx)("div",{children:(0,n.jsxs)("p",{children:["This is a black box function. Read ",(0,n.jsx)(i.A,{to:e,children:"this section"})," to learn more about black box functions in Noir."]})})}},28453(e,r,t){t.d(r,{R:()=>a,x:()=>o});var i=t(96540);const n={},s=i.createContext(n);function a(e){const r=i.useContext(s);return i.useMemo(function(){return"function"==typeof e?e(r):{...r,...e}},[r,e])}function o(e){let r;return r=e.disableParentContext?"function"==typeof e.components?e.components(n):e.components||n:a(e.components),i.createElement(s.Provider,{value:r},e.children)}}}]);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.